package com.wswy.wzcx.ui.dmv

import android.content.ContentValues
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import com.wswy.wzcx.model.dmv.DepartmentInfo

internal class DataProvider (private val context: Context ){

    companion object {
        private const val DB_NAME = "city_dvm"
        private const val TB_DMV = "tb_dmv"
        private val columns = arrayOf("id","cityId","address","name","scope","tel","workTime","lat","lon")
    }

    private val sqLiteDatabase: SQLiteDatabase by lazy {
        context.applicationContext.openOrCreateDatabase(DB_NAME,Context.MODE_PRIVATE,null).also {
            it?.execSQL(" CREATE TABLE if not exists $TB_DMV (id int primary key,cityId int," +
                    "address text,name text,scope text,tel text,workTime text,lat real,lon real)")
        }
    }


    fun query(cityId:Int):List<DepartmentInfo>{


        sqLiteDatabase.query(TB_DMV, columns," cityId = ? ", arrayOf(cityId.toString()),null,null,null)?.use {
            val list = arrayListOf<DepartmentInfo>()

            while (it.moveToNext()){
                val data =DepartmentInfo()
                data.id = it.getInt(it.getColumnIndex("id"))
                data.address = it.getString(it.getColumnIndex("address"))
                data.name = it.getString(it.getColumnIndex("name"))
                data.scope = it.getString(it.getColumnIndex("scope"))
                data.tel = it.getString(it.getColumnIndex("tel"))
                data.workTime = it.getString(it.getColumnIndex("workTime"))
                data.lat = it.getDouble(it.getColumnIndex("lat"))
                data.lon = it.getDouble(it.getColumnIndex("lon"))

            }
            return list
        }

        return emptyList()
    }

    private fun save(cityId: Int,list: List<DepartmentInfo>?){


        list?.apply {

            sqLiteDatabase.transaction {

                val contentValues= ContentValues()
                forEach {

                    contentValues.clear()
                    contentValues.put("cityId",cityId)
                    contentValues.put("id",it.id)
                    contentValues.put("address",it.address)
                    contentValues.put("name",it.name)
                    contentValues.put("scope",it.scope)
                    contentValues.put("tel",it.tel)
                    contentValues.put("workTime",it.workTime)
                    contentValues.put("lat",it.lat)
                    contentValues.put("lon",it.lon)
                    sqLiteDatabase.insertWithOnConflict(TB_DMV,"id",contentValues,SQLiteDatabase.CONFLICT_REPLACE)
                }

            }

        }


    }




    inline fun <T> SQLiteDatabase.transaction(
            exclusive: Boolean = true,
            body: SQLiteDatabase.() -> T
    ): T {
        if (exclusive) {
            beginTransaction()
        } else {
            beginTransactionNonExclusive()
        }
        try {
            val result = body()
            setTransactionSuccessful()
            return result
        } finally {
            endTransaction()
        }
    }
}